/*!
 * Table Page Template v0.0.1
 * Author: thon.ju
 * Email: thon.ju@gmail.com
 * Date: 2013-09-05 13:09
 * Description: display data by table page
 */

define(function(require, exports, module){
	
	var $ = require('jquery');
	require('artTemplate/artTemplate/2.0.1/template.js');
	
	template.openTag = '<$';
	template.closeTag = '$>';
	
	// 参数
	exports.options = {
		listLink : '',
		lockLink : '',
		delLink : '',
		editLink : '',
		addLink : '',
		listParams :'{"p":"1", "s":"10"}'
	};
	
	/*==加载数据=======================================================================*/
	exports.loadPage = function loadPage() {
		/* Send the data using post */
		var getting = $.get(exports.options.listLink, exports.options.listParams);

		/* Put the results in a div */
		getting.done(function(data) {
			var html = template.render('list-template', data);
			
			// 显示数据
			$("#list-data").empty().append(html);
			
			// 增加一条数据
			$('.add-click').click(function(){
				exports.addClick(this);
			});
			
			// 修改一条数据状态
			$('.lock-click').click(function(){
				exports.lockClick(this);
			});
			
			// 修改一条数据
			$('.edit-click').click(function(){
				exports.editClick(this);
			});
			
			// 删除一条数据
			$('.del-click').click(function(){
				exports.delClick(this);
			});
			
			// 分页
			$('.pagination li a').click(function(){
				var data = $.parseJSON($(this).attr("data"));
				
				exports.options.listParams.p=data.p;
				exports.options.listParams.s=data.s;
				exports.loadPage();
			});
		});
	};
	
	
	/*==修改一条数据状态=======================================================================*/
	exports.lockClick = function lockClick(obj) {
		var data = $.parseJSON($(obj).attr("data"));
		var html = template.render('lock-template', data);
		$('#lock-data').empty().append(html);
		$('#lock-modal').modal();
		
		$('.lock-submit').click(function(){
			exports.submitLock();
		});
	};
	
	exports.submitLock = function submitLock() {
		var $form = $("#lock-form");
		/* Send the data using post */
		var posting = $.post(exports.options.lockLink, $form.serialize());
		
		/* Put the results in a div */
		posting.done(function(data) {
			$('#lock-modal').modal('hide');
			var data = $.parseJSON($('.pagination .active a').attr("data"));
			
			exports.options.listParams.p=data.p;
			exports.options.listParams.s=data.s;
			exports.loadPage();
		});
		
	};
	
	/*==增加一条数据=======================================================================*/
	exports.addClick = function lockClick(obj) {
		var html = template.render('add-template');
		$('#add-data').empty().append(html);
		$('#add-modal').modal();
		
		$('.add-submit').click(function(){
			exports.submitAdd();
		});
	};
	
	exports.submitAdd = function submitLock() {
		var $form = $("#add-form");
		/* Send the data using post */
		var posting = $.post(exports.options.addLink, $form.serialize());
		
		/* Put the results in a div */
		posting.done(function(data) {
			$('#add-modal').modal('hide');
			var data = $.parseJSON($('.add-click').attr("data"));
			
			exports.options.listParams.p=data.p;
			exports.options.listParams.s=data.s;
			exports.loadPage();
		});
		
	};
	
	/*==修改一条数据内容=======================================================================*/
	exports.editClick = function lockClick(obj) {
		var data = $.parseJSON($(obj).attr("data"));
		var html = template.render('edit-template', data);
		$('#edit-data').empty().append(html);
		$('#edit-modal').modal();
		
		$('.edit-submit').click(function(){
			exports.submitEdit();
		});
	};
	
	exports.submitEdit = function submitLock() {
		var $form = $("#edit-form");
		/* Send the data using post */
		var posting = $.post(exports.options.editLink, $form.serialize());
		
		/* Put the results in a div */
		posting.done(function(data) {
			$('#edit-modal').modal('hide');
			var data = $.parseJSON($('.pagination .active a').attr("data"));
			
			exports.options.listParams.p=data.p;
			exports.options.listParams.s=data.s;
			exports.loadPage();
		});
		
	};
	
	/* 删除用户 */
	exports.delClick = function delClick(obj) {
		var isDelete = confirm("删除该用户，确认？");
		if (isDelete) {
			var data = $.parseJSON($(obj).attr("data"));
			var posting = $.post(exports.options.delLink, {id : data.id});
			/* Put the results in a div */
			posting.done(function(data) {
				var tr = obj.parentNode.parentNode;
				var tbody = tr.parentNode;
				tbody.removeChild(tr);
			});
		};
	};
});
